/*****************************************************************************/
/*                                                                           */
/*    Domino Operation System System Specific Module                         */
/*                                                                           */
/*    Copyright (C) 2010 Laszlo Arvai                                        */
/*                                                                           */
/*    ------------------------------------------------------------------     */
/*    netPacketBufer - Network packet buffer implementation                  */
/*****************************************************************************/

#ifndef __netPacketBuffer_h
#define __netPacketBuffer_h
 
///////////////////////////////////////////////////////////////////////////////
// Includes
#include <krnlTypes.h>
#include <netSocket.h>
#include <netPacketBufferType.h>
#include <krnlString.h>

///////////////////////////////////////////////////////////////////////////////
// Const

//! Packet types
typedef enum _netPacketType
{
  netPT_UNKNOWN,    //!< Unknown packet type
  netPT_IP,         //!< IP packet type
  netPT_LCP,        //!< PPP Link control protocol (LCP)
  netPT_PAP,        //!< PPP Password authentication protocol (PAP)
  netPT_CHAP,       //!< PPP Challenge handshake authentication protocol (CHAP)
  netPT_IPCP,       //!< PPP Internet protocol control protocol (IPCP)
  netPT_UknownPPP		//!< Unknown protocol over PPP
} netPacketType;

// Buffer size constants
#define netCHUNK_SIZE   64    //!< Size of one chunk (buffer allocation granularity)
#define netCHUNK_COUNT	64    //!< Number of available chunks

#define netINVALID_CHUNK    0xff

///////////////////////////////////////////////////////////////////////////////
// Types

// Chunk status codes
typedef enum _netChunkStatus
{
  netCS_EMPTY,
  netCS_RESERVED,
  netCS_PACKET_END
} netChunkStatus;

// packet info
typedef struct _netPacketInfo
{
  netChunkIndex PayloadChunkIndex;
  dosByte PayloadChunkPos;
  dosWord PayloadLength;
  dosWord RemotePort;
  IN_ADDR Address;
} netPacketInfo;

// packet entry
typedef struct _netPacketEntry
{
	netChunkStatus Status:4;			//< Chunk status
	netPacketType Type:4;					//< Packet type
	netChunkIndex NextChunk;
	dosByte UsedLength;
	dosByte id;
	dosByte Buffer[netCHUNK_SIZE];
} netPacketEntry;

///////////////////////////////////////////////////////////////////////////////
// Global variables
extern netPacketEntry g_netBuffer[];

///////////////////////////////////////////////////////////////////////////////
// Function prototypes
void netBufferInitialize(void);
dosWord netGetFreeBufferSize(void);

// Packet storage - RX functions
dosBool netCreateRXPacket( dosByte in_type );
dosBool netWriteByteRXPacket( dosByte in_data );
dosByte netStoreRXPacket(void);
dosByte netStoreAndSetLengthRXPacket(dosWord in_length);
void netCancelRXPacket(void);

// Packet retieval - RX functions
dosBool netIsRXPacketAvailable(void);
dosWord netGetRXPacketLength(void);
netPacketType netGetAvailablePacketType(void);
dosBool netOpenRXPacket(void);
dosBool netReadByteRXPacket( dosByte* out_byte );
dosBool netReadBlockRXPacket( dosByte* out_buffer, dosWord in_length );
void netDeleteRXPacket(void);
netChunkIndex netUnlinkRXPacket(void);
void netUpdateRXPacketChecksum( dosWord in_length, dosDWord* inout_checksum );
																																						 
dosWord netGetRXPacketPos(void);
dosBool netSetRXPacketPos( dosWord in_pos );

dosByte* netGetRXPacketChunkBuffer(void);

// Socket packet retrieval functions
dosBool netOpenSocketRXPacket( netSOCKET in_socket, netPacketInfo* out_packet_info );
void netDeleteSocketRXPacket(netSOCKET in_socket);

// Packet retrieval - TX functions
dosBool netIsTXPacketAvailable(void);
dosBool netIsTXPacketStart(void);
dosBool netReadByteTXPacket( dosByte* out_byte );
dosBool netReadWordRXPacket( dosWord* out_word );
dosBool netReadDWordRXPacket( dosDWord* out_word );
netPacketType netGetTXPacketType(void);

// Packet storage - TX function
dosBool netOpenTXPacket( dosByte in_chunk_index );
dosBool netCreateTXPacket(netPacketType in_type);
void netCancelTXPacket(void);
dosByte* netGetTXPacketChunkBuffer(void);
dosBool netSetTXPacketPos(dosWord in_pos );
dosWord netGetTXPacketPos(void);
dosByte netWriteByteTXPacket( dosByte in_data );
dosBool netWriteWordTXPacket(dosWord in_data);
dosBool netWriteDWordTXPacket(dosDWord in_data);
dosBool netWriteBlockTXPacket( dosByte* in_buffer, dosWord in_length );
dosStringLength netWriteConstStringTXPacket( dosConstString in_buffer, dosBool* in_success );
dosStringLength netWriteStringTXPacket( dosString in_buffer, dosBool* in_success );
void netUpdateTXPacketChecksum( dosWord in_length, dosDWord* inout_checksum);
dosBool netRetrieveByteTXPacket( dosByte* out_data );

// Packet storage - Other functions
void netLinkTXPacket( netChunkIndex in_chunk_index );
dosBool netSendTXPacket(void);
netChunkIndex netDeletePacket(netChunkIndex in_chunk);

// Other functions
void netMoveRXPacketToSocketBuffer( netSOCKET in_socket, netPacketInfo* in_packet_info );

#endif

